package com.bykj.taketurns.widget;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewConfigurationCompat;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

import com.bykj.taketurns.R;

/**
 * Created by Administrator on 2017/8/29.
 */

public class PersonalRecyclerview extends RecyclerView{
    private float mInitialMotionX;
    private float mInitialMotionY;
    private int mTouchSlop;
    private Activity mContext;
    public PersonalRecyclerview(Context context) {
        this(context,null);
    }

    public PersonalRecyclerview(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PersonalRecyclerview(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext= (Activity) context;
        ViewConfiguration configuration = ViewConfiguration.get(context);
        // 获取TouchSlop值
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                final float x = event.getX();
                final float y = event.getY();
                mInitialMotionX = x;
                mInitialMotionY = y;
                Log.d("zzz","downper");
                return super.onTouchEvent(event);
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                final float upX = event.getX();
                final float upY= event.getY();
                final float dx = upX - mInitialMotionX;
                final float dy = upY - mInitialMotionY;
                Log.d("zzz","open");
                if(dx>mTouchSlop){
                    if(dx>Math.abs(dy)&&dx>0) {
                        mContext.finish();
                        mContext.overridePendingTransition(R.anim.trans_left_in, R.anim.trans_right_out);
                        break;
                    }else {
                        return super.onTouchEvent(event);
                    }
                }


        }
        return super.onTouchEvent(event);
    }
}
